package gipfProject.games.zertz.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import gipfProject.tools.ResourceLoader;
import gipfProject.games.zertz.data.PieceData;
import gipfProject.games.zertz.data.ZertzData;

public class ScoreUI extends JPanel{
	ZertzData gameState;
	ResourceLoader images;

	Color backgroundColor;
	Color playerColor;
	Color textColor;
	
	private int player;
	
	static int
		xOffset = 20,
		yOffset = 23,
		pieceHeight = 20,
		pieceWidth  = 45;
	
	ScoreUI(ResourceLoader images, ZertzData gameState, int player) {
		this.gameState = gameState;
		this.images = images;
		this.player = player;
		
		backgroundColor = Color.blue;
		
		if (player == ZertzData.playerWhite)
		{
			playerColor = Color.white;
			textColor   = Color.black;
		}else{
			playerColor = Color.black;
			textColor   = Color.white;
		}
	}

	public void paintComponent(Graphics g) {

		//Erase the previously-drawn graphics
		g.setColor(Color.blue );
		g.fillRect(0, 0, getSize().width,getSize().height);

		//Draw a border around the board
		g.setColor(Color.black);
		g.drawRect(0,0,getSize().width-1,getSize().height-1);
		g.drawRect(1,1,getSize().width-3,getSize().height-3);

		//Draw the name box
		g.setColor( playerColor );
		g.fillRect(2, 2, getSize().width-4,28);

		//Paint a border on it
		g.setColor( Color.black );
		g.drawLine(0, 30, getSize().width, 30);
		g.drawLine(0, 31, getSize().width, 31);

		//And write the caption
		g.setColor( textColor );
		g.drawString(gameState.getName(player) + "'s captures:",8,16);

		//Visually represent the score
		drawPool(g);
		
		//Indicate the color for each pool
		
		//Text display
		
		g.setColor( Color.white );
		Font textFont = new Font("Helvetica", Font.BOLD, 14);
		g.setFont(textFont);
		
		if (player == ZertzData.playerWhite)
		{
			g.drawString(gameState.getScore(player,PieceData.WHITE) + "/" + gameState.getGoal(PieceData.WHITE), xOffset+6+(pieceWidth*2), 205);
			g.drawString(gameState.getScore(player,PieceData.GRAY ) + "/" + gameState.getGoal(PieceData.GRAY),  xOffset+6+(pieceWidth*1), 205);
			g.drawString(gameState.getScore(player,PieceData.BLACK) + "/" + gameState.getGoal(PieceData.BLACK), xOffset+6+(pieceWidth*0), 205);
			g.drawImage(images.getResource("WhiteRing"), xOffset+(pieceWidth*2), 220, this);
			g.drawImage(images.getResource("GrayRing") , xOffset+(pieceWidth*1), 220, this);
			g.drawImage(images.getResource("BlackRing"), xOffset+(pieceWidth*0), 220, this);
		}else{
			g.drawString(gameState.getScore(player,PieceData.WHITE) + "/" + gameState.getGoal(PieceData.WHITE), xOffset+6+(pieceWidth*0), 205);
			g.drawString(gameState.getScore(player,PieceData.GRAY ) + "/" + gameState.getGoal(PieceData.GRAY),  xOffset+6+(pieceWidth*1), 205);
			g.drawString(gameState.getScore(player,PieceData.BLACK) + "/" + gameState.getGoal(PieceData.BLACK), xOffset+6+(pieceWidth*2), 205);
			g.drawImage(images.getResource("WhiteRing"), xOffset+(pieceWidth*0), 220, this);
			g.drawImage(images.getResource("GrayRing") , xOffset+(pieceWidth*1), 220, this);
			g.drawImage(images.getResource("BlackRing"), xOffset+(pieceWidth*2), 220, this);
		}
		

	}


	private String getPieceFile(int pieceColor){
		if (pieceColor == PieceData.WHITE) {
			return "WhiteMarble";
		} else if (pieceColor == PieceData.GRAY) {
			return "GrayMarble";
		} else if (pieceColor == PieceData.BLACK) {
			return "BlackMarble";
		} else {
			return null;
		}
	}

	private void drawPool(Graphics g){
		//Draw one of each ring for selection purposes		
		for (int pieceColor = 0 ; pieceColor < 3 ; pieceColor++){
			for (int i = 0; i < gameState.getGoal(pieceColor); i++){
				
				//Determine if this is an occupied or empty ring and get the graphic file
				Image tempImage;
				if (i < gameState.getScore(player,pieceColor)){
					tempImage = images.getResource(getPieceFile(pieceColor));
				}else{
					tempImage = images.getResource("EmptyMarble");
				}
				
				//Determine if we are flipping the display along the x-axis
				int xInvert;
				
				if (player == ZertzData.playerWhite)
				{
					xInvert = pieceWidth * (2-pieceColor);
				}else{
					xInvert = pieceWidth * pieceColor;
				}
				
				//Draw the acquired graphics file
				
				g.drawImage(tempImage, xOffset+8+xInvert, 60+(pieceHeight*(5-i)), this);
			}
		}
	}
}
